/**   
 * @Title: RechargeShenzhouxin.java 
 * @Package com.esun.elk.ui.views.userinfo.recharge 
 * @Description: 神州行充值卡页
 * @author 孔祥攀   
 * @date 2012-4-12 上午9:31:11  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.cheetah.view.personal.recharg;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.recharg.SzxRechargCommunicater;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.view.setting.connect.ConnectionChangeReceiver;
import com.esun.util.EditTextInputUitl;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * 
 * @Package com.esun.cheetah.view.personal.recharg.RechargeShenzhouxin
 * @ClassName: RechargeShenzhouxin
 * @Description: 神州行充值卡页
 * @author 孔祥攀 <br/>
 * @date 2012-4-12 上午9:31:11
 * 
 */
public class RechargeShenzhouxin extends Activity implements OnClickListener,
		OnCheckedChangeListener, OnSeekBarChangeListener, OnTouchListener {

	private RadioGroup radioPrice;
	private EditText etCardNum;
	private Button btnCardNumClear;
	private EditText etCardPwd;
	private Button btnCardPwdClear;
	private TextView tvHowmany;
	private TextView tvRealMoney;
	private TextView tvYDLT1;// 移动联通温馨提示
	private TextView tvYDLT2;
	private TextView tvYDLT3;
	private TextView tvYDLT4;
	private Button btnGoBack;
	private Button btnRecharge;
	private SeekBar seek;
	public static Handler handlerDoRecharge;
	private SzxRechargCommunicater doSzxRechargCommunicater;
	private final int PRICE_30 = 0;
	private final int PRICE_50 = 1;
	private final int PRICE_100 = 2;
//	private boolean isRechargSuccess = false;
	private String cardType;
	private String cardAmount;
	private RelativeLayout relativeLayoutMain;
	private InputMethodManager imm;
	private LinearLayout linearLayoutMain;
	private ProgressDialog progress;

	// private SzxRechargCommunicater controller = new
	// SzxRechargCommunicater(null, cardAmount, cardAmount, cardAmount,
	// cardAmount);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recharge_szx);
		// 防止进入键盘弹出
		((TextView) findViewById(R.id.id_of_TextView)).requestFocus();
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		initPage();
		initHandler();
	}

	private void initPage() {

		cardType = "1";
		cardAmount = "50";

		btnGoBack = (Button) this.findViewById(R.id.recharge_szx_btn_goBack);
		btnGoBack.setOnClickListener(this);
		btnRecharge = (Button) this.findViewById(R.id.recharge_szx_btn_submit);
		btnRecharge.setOnClickListener(this);

		etCardNum = (EditText) this.findViewById(R.id.recharge_szx_et_cardNum);
		btnCardNumClear = (Button) this
				.findViewById(R.id.recharge_szx_et_cardNum_btn_claen);
		etCardNum = new EditTextInputUitl(etCardNum, btnCardNumClear)
				.getEditText();

		etCardPwd = (EditText) this.findViewById(R.id.recharge_szx_et_cardPwd);
		btnCardPwdClear = (Button) this
				.findViewById(R.id.recharge_szx_et_cardPwd_btn_claen);
		etCardPwd = new EditTextInputUitl(etCardPwd, btnCardPwdClear)
				.getEditText();

		seek = (SeekBar) this.findViewById(R.id.recharge_szx_seek_price);
		seek.setOnSeekBarChangeListener(this);

		tvHowmany = (TextView) this
				.findViewById(R.id.recharge_szx_tv_cardPrice);
		tvRealMoney = (TextView) this
				.findViewById(R.id.recharge_szx_tv_realMoney);

		tvYDLT1 = (TextView) this.findViewById(R.id.ydlt_cz_id1);
		tvYDLT2 = (TextView) this.findViewById(R.id.ydlt_cz_id2);
		tvYDLT3 = (TextView) this.findViewById(R.id.ydlt_cz_id3);
		tvYDLT4 = (TextView) this.findViewById(R.id.ydlt_cz_id4);

		radioPrice = (RadioGroup) this.findViewById(R.id.recharge_szx_cardType);
		radioPrice.setOnCheckedChangeListener(this);
		radioPrice.check(R.id.recharge_szx_radio_movemnet);
		seek.setProgress(1);

		progress = ViewerUtil.showProgressDialog(this, "提交中...");

		relativeLayoutMain = (RelativeLayout) findViewById(R.id.rechargeszx_relative_main);
		relativeLayoutMain.setOnTouchListener(this);
		linearLayoutMain = (LinearLayout) findViewById(R.id.rechargeszx_linear_main);
		linearLayoutMain.setOnTouchListener(this);
		imm = (InputMethodManager) this.getSystemService(INPUT_METHOD_SERVICE);

	}

	public void startRechargeCommunicate() {
		doSzxRechargCommunicater = new SzxRechargCommunicater(
				handlerDoRecharge, cardAmount, etCardNum.getText().toString(),
				etCardPwd.getText().toString(), cardType);
		doSzxRechargCommunicater.startCommunicate();
	}

	/**
	 * @Title: initHandler
	 * @Description: Handler初始化
	 * @param
	 * @return
	 * @throws
	 */
	public void initHandler() {
		handlerDoRecharge = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.cancel();

				switch (msg.what) {

				// 充值成功
				case Config.COMMUNICATER_SUCCESS:
//					isRechargSuccess = true;
					ViewerUtil.showRechargSZXDialog(RechargeShenzhouxin.this);
					break;

				// 充值失败
				case Config.COMMUNICATER_FAIL:
//					isRechargSuccess = false;
					ViewerUtil.showTipDialog(RechargeShenzhouxin.this,
							doSzxRechargCommunicater.getServerMsg(), false);
					EDebug.o("$$$$$$$$$$"
							+ doSzxRechargCommunicater.getServerMsg());
					break;

				// 发生异常
				case Config.COMMUNICATER_EXCEPTION:
					ViewerUtil.showTipDialog(RechargeShenzhouxin.this,
							Language.MSG_CONNECTIONFAIL, false);
					break;
				}
			}
		};
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.recharge_szx_btn_goBack:

			finish();

			break;

		case R.id.recharge_szx_et_cardNum:

			etCardNum.setText("");

			break;
		case R.id.recharge_szx_et_cardPwd:

			etCardPwd.setText("");

			break;

		case R.id.recharge_szx_btn_submit:

			if (etCardNum.getText().toString().length() > 1
					&& etCardPwd.getText().toString().length() > 1) {

				progress.show();
				startRechargeCommunicate();

			} else {
				ViewerUtil.showTipDialog(this, "请正确输入充值卡信息！", false);
			}
			break;
		}
	}
//将menu键点击事件返回给父activity处理
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   // TODO Auto-generated method stub
   if (keyCode == KeyEvent.KEYCODE_MENU) {
     Activity parent = getParent();
     if (parent != null) {
       parent.onKeyDown(keyCode, event);
       return true;
     }
   }
   return super.onKeyDown(keyCode, event);
 }


	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {

		switch (checkedId) {

		case R.id.recharge_szx_radio_movemnet:

			cardType = "0";
			tvYDLT1.setText("1.中国移动充值卡充值方式支持全国通用的移动充值卡");
			tvYDLT2.setText("2.手续费的特惠:充值享受优惠费率3%,实际到账97%");
			tvYDLT3.setText("3.充值卡内金额将一次性全额扣除,请选择与您的充值卡面额相同的金额");
			tvYDLT4.setText("4.由于财务结算，每日23:45-00:10暂停充值");

			break;

		case R.id.recharge_szx_radio_uninet:

			cardType = "1";
			tvYDLT1.setText("1.中国联通充值卡充值方式支持全国通用的联通一卡充");
			tvYDLT2.setText("2.因中国联通相关政策,使用联通充值卡充值,您需向服务商缴纳手续费,金额为您所选择的充值卡面额的3%,该费用将直接从您的充值卡中扣除,实际到账97%");
			tvYDLT3.setText("3.充值卡内金额将一次性全额扣除,请选择与您的充值卡面额相同的金额");
			tvYDLT4.setText("4.由于财务结算，每日23:45-00:10暂停充值");
			break;

		}

	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {

		switch (progress) {
		case PRICE_30:

			tvHowmany.setText("30");
			cardAmount = "30";
			tvRealMoney.setText("29.10");

			break;

		case PRICE_50:

			tvHowmany.setText("50");
			cardAmount = "50";
			tvRealMoney.setText("48.50");

			break;

		case PRICE_100:

			tvHowmany.setText("100");
			cardAmount = "100";
			tvRealMoney.setText("97.00");

			break;
		}

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {

	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver
				.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.registerReceiver(
				ConnectionChangeReceiver.getConnectionChangeReceiver(),
				ConnectionChangeReceiver.getIntentFilter());
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		imm.hideSoftInputFromWindow(etCardNum.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(etCardPwd.getWindowToken(), 0);
		return false;
	}

}
